function setCookie(c_name,value,exdays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name+"_save" + "=" + c_value;
}
function getCookie(c_name){
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++){
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name+"_save"){
			return unescape(y);
		}
	}
}
function genkey(lenght){
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    for( var i=0; i < lenght; i++ ){
        text += possible.charAt(Math.floor(Math.random() * possible.length));
	}
    return text;
}
var API = {
		'hosturl':'http://localhost/InnerServer/Interface',
	'lastResult':'',
	'resultListener':null,
	'account':{
		'ID':'',
		'EmailAddress':''
	},
	'request':function(param){
		this.lastResult = sys_http_getData(this.hosturl + '/service.php?method=service',param,'post');
		if(this.resultListener!=null)this.resultListener(this.getResult(this.lastResult));
		return this.lastResult;
	},
	'request':function(method,param){
		this.lastResult = sys_http_getData(this.hosturl + '/service.php?method=service&request='+ method,param,'post');
		if(this.resultListener!=null)this.resultListener(this.getResult(this.lastResult));
		return this.lastResult;
	},
	'request':function(method,lang,param){
		this.lastResult = sys_http_getData(this.hosturl + '/service.php?method=service&request='+ method + '&lang='+lang,param ,'post');
		if(this.resultListener!=null)this.resultListener(this.getResult(this.lastResult));
		return this.lastResult;
	},
	'getResultStatus':function(result_xml){
		var result_doc = sys_str_toXMLdoc(sys_str_getXMLstr(result_xml,"result"));
		if(result_doc.getAttribute){
			return result_doc.getAttribute('status');
		}
		return "false";
	},
	'getResultMessage':function(result_xml,message_name){
		var result_doc = sys_str_toXMLdoc(sys_str_getXMLstr(result_xml,"result"));
		if(result_doc.childNodes && result_doc.childNodes.length > 0){
			for(var i=0;i<result_doc.childNodes.length;i++){
				if(result_doc.childNodes[i].nodeName =='message'){
					var message_nodes = result_doc.childNodes[i];
					if(message_nodes.childNodes && message_nodes.childNodes.length > 0){
						for(var j = 0; j< message_nodes.childNodes.length; j++){
							if(message_nodes.childNodes[j].nodeName == 'item' && message_nodes.childNodes[j].getAttribute('name') == message_name){
								return sys_node_getText(message_nodes.childNodes[j]);
							}
						}
					}
				}
			}
		}
		return "";
	},
	'getResultMessages':function(result_xml){
		var rs = new Array();
		var result_doc = sys_str_toXMLdoc(sys_str_getXMLstr(result_xml,"result"));
		if(result_doc.childNodes && result_doc.childNodes.length > 0){
			for(var i=0;i<result_doc.childNodes.length;i++){
				if(result_doc.childNodes[i].nodeName =='message'){
					var message_nodes = result_doc.childNodes[i];
					if(message_nodes.childNodes && message_nodes.childNodes.length > 0){
						for(var j = 0; j< message_nodes.childNodes.length; j++){
							if(message_nodes.childNodes[j].nodeName == 'item'){
								rs[rs.length] = {'name':message_nodes.childNodes[j].getAttribute("name"),'message':sys_node_getText(message_nodes.childNodes[j])};
							}
						}
					}
				}
			}
		}
		return rs;
	},
	'getResultErrors':function(result_xml){
		var rs = new Array();
		var result_doc = sys_str_toXMLdoc(sys_str_getXMLstr(result_xml,"result"));
		if(result_doc.childNodes && result_doc.childNodes.length > 0){
			for(var i=0;i<result_doc.childNodes.length;i++){
				if(result_doc.childNodes[i].nodeName =='error'){
					var error_nodes = result_doc.childNodes[i];
					if(error_nodes.childNodes && error_nodes.childNodes.length > 0){
						for(var j = 0; j< error_nodes.childNodes.length; j++){
							if(error_nodes.childNodes[j].nodeName == 'item'){
								rs[rs.length] = {'code':error_nodes.childNodes[j].getAttribute("code"),'message':sys_node_getText(error_nodes.childNodes[j])};
							}
						}
					}
				}
			}
		}
		return rs;
	},
	'getResult':function(result_xml){
		var rs = {
			'status':this.getResultStatus(result_xml),
			'errors':this.getResultErrors(result_xml),
			'messages':this.getResultMessages(result_xml)
		};
		return rs;
	},
	'checkLogin':function(){
		var status = this.getResultStatus(this.request("checkLogin","xml",""));
		return (status == 'true');
	},
	'login':function(email_address,password){
		var param = "email_address=" + email_address + "&password=" + password;
		var rs = this.request("login","xml",param);
		var status = this.getResultStatus(rs);
		if(status == 'true'){
			this.account.ID = this.getResultMessage(rs,'userid');
			this.account.EmailAddress = email_address;
			setCookie("email_address",this.account.EmailAddress,30);
		}
		return(status == 'true');
	},
	'loginWithParam':function(param){
		var status = this.getResultStatus(this.request("login","xml",param));
		return(status == 'true');
	},
	'logout':function(){
		this.removeCertificate();
		if(this.getResultStatus(this.request("logout","")) == "true"){
			return true;
		}
		return false;
	},
	'getMacAddress':function(){
		var mac_address = getCookie("mac_address");
		if(mac_address != null && mac_address != ""){
		}else{
			mac_address = genkey(10);
			setCookie("mac_address",mac_address,30);
		}
		return mac_address;
	},
	'getUserID':function(){
		return this.account.ID;
	},
	'getUserEmailAddress':function(){
		if(this.account.EmailAddress != ''){
			return this.account.EmailAddress;
		}else{
			var mail = getCookie("email_address");
			if(mail != null && mail != ''){
				return mail;
			}
		}
		return '';
	},
	'getCertificate':function(){
		var email_address = getCookie("email_address");
		if(email_address != null && email_address != ''){
			var mac_address = this.getMacAddress();
			var phone_id = genkey(10);
			setCookie("mac_address",mac_address,30);
			setCookie("phone_id",phone_id,30);
			setCookie("email_address",email_address,30);
			var rs_xml = this.request("getCertificate","xml","mac_address="+mac_address+"&device_id=" + phone_id + "&email_address=" + email_address);
			var certificate = this.getResultMessage(rs_xml,"certificate");
			if(certificate != ""){
				setCookie("certificate",certificate,30);
			}
			return certificate;
		}
	},
	'removeCertificate':function(){
		var rs = this.request('removeCertificate',"xml",'mac_address='+this.getMacAddress());
		return (this.getResultStatus(rs) == "true");
	},
	'loginByCertificate':function(){
		var certificate = getCookie("certificate");
		if(certificate != null && certificate != ""){
			var mac_address = getCookie("mac_address");
			var phone_id = getCookie("phone_id");
			var email_address = getCookie("email_address");
			if(mac_address != null && mac_address != "" && phone_id != null && phone_id != ""){
				var rs = this.request("loginByCertificate","xml","mac_address=" + mac_address
				+ "&device_id=" + phone_id + "&certificate=" + certificate +"&email_address=" + email_address);
				this.account.EmailAddress = email_address;
				if(this.getResultStatus(rs) == 'true'){
					this.account.ID = this.getResultMessage(rs,'userid');
					return true;
				}
			}
		}
		return false;
	},
	'changePassword':function(){
	},
	'end':''
};